Clase 5: Shiny

Diplomado en Data Science para las Ciencias Sociales

Andrés González-Santa Cruz

Estudiante Doctorado en Salud Pública, Investigador joven, nDP

15 de dic, 2024

Recordemos

expandir para código
# Activar showtext
showtext_auto()

# Agregar la fuente Oswald desde Google Fonts
font_add_google(name = "Oswald", family = "Oswald")

library(ggplot2)

# Crear los datos
data <- data.frame(
  xstart = c(0, 0.2, 0.4, 0.6, 0.8),
  xend = c(0.2, 0.4, 0.6, 0.8, 1),
  ystart = rep(0, 5),
  yend = rep(1, 5),
  labels = c("Solo publicación", "Código", "Código y datos",
             "Código y datos\nejecutables", "Replicación\ncompleta")
)

# Colores personalizados desde oscuro a claro
#colors <- c("#555555", "#777777", "#999999", "#bbbbbb", "#dddddd")
colors <- c("#4D4D4D", "#B03A2E", "#E74C3C", "#F1948A", "#FADBD8")

# Crear el gráfico
ggplot() +
  # Dibujar las áreas
    geom_rect(data = data, aes(xmin = xstart, xmax = xend, ymin = ystart, ymax = yend, fill = labels), color = NA) +
  scale_fill_manual(values = colors) +
  # Agregar las etiquetas dentro de las áreas
  geom_text(data = data, aes(x = (xstart + xend) / 2, y = 0.5, label = labels), size = 6, family = "Oswald", color = "black") +
  # Agregar flecha de reproducibilidad
  annotate("segment", x = 0, xend = 1, y = -0.1, yend = -0.1, arrow = arrow(type = "closed", ends = "both"), color = "gray") +
  annotate("text", x = 0.1, y = -0.15, label = "No\nreproducible", size = 5, hjust = 0.5, color = "#555555") +
  annotate("text", x = .9, y = -0.15, label = "Estándar\nde oro", size = 5, hjust = 0.5, color = "#555555") +
  # Ajustar límites y tema
  scale_x_continuous(limits = c(0, 1), expand = c(0, 0)) +
  scale_y_continuous(limits = c(-0.2, 1), expand = c(0, 0)) +
  theme_void() +
  theme(
    legend.position = "none",
    text = element_text(family = "Oswald"),
    plot.background = element_rect(fill = "#ffffff", color = NA)
  )
Esquema

Esquema

Qué es Shiny?

  • Shiny es un paquete de R que permite construir aplicaciones web interactivas.
  • Ideal para la visualización de datos y análisis interactivos.
  • No requiere conocimientos avanzados de HTML o JavaScript.
  • Permite transformar análisis en herramientas interactivas.
  • Fácil integración en presentaciones con Quarto.
  • Ideal para comunicar hallazgos de manera dinámica.

Ejemplo

expandir para código
if(!require(shiny)){install.packages("shiny")}

ui <- fluidPage(
  titlePanel("Ejemplo Shiny"),
  sidebarLayout(
    sidebarPanel(
      sliderInput("num", "Elige un número:", 1, 100, 50)
    ),
    mainPanel(
      plotOutput("plot")
    )
  )
)

server <- function(input, output) {
  output$plot <- renderPlot({
    hist(rnorm(input$num), col = "red", border = "white")
  })
}

shinyApp(ui, server)

Shiny applications not supported in static R Markdown documents

Algunos ejemplos externos

Componentes

  • UI
  • SERVER

Manos a la obra

Fuentes

  • Mendez Carlos (2020). An interactive exploration of panel data: Using the package ExPanDaR to generate interactive web applications. R Studio/RPubs. Available at https://rpubs.com/quarcs-lab/explore-panel-interactively
  • Chang, W., Borges Ribeiro, B., & Grolemund, G. (2023). Mastering Shiny: Building Interactive Applications in R. O’Reilly Media.
  • Wickham, H., Chang, W., Henry, L., Pedersen, T., Takahashi, K., Wilke, C., … & Yutani, H. (2023). Shiny: Web Application Framework for R. Disponible desde https://shiny.rstudio.com/.

timer.utf8